package com.jason.cloud.engine.share.driver.enterprise.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.email.bean.EmailBean;
import com.jason.cloud.engine.share.driver.enterprise.bean.EnterpriseBean;
import com.jason.cloud.engine.share.driver.enterprise.service.EnterpriseService;
import com.jason.cloud.engine.share.driver.pack.bean.PackBean;
import com.jason.cloud.engine.share.driver.pack.service.PackService;
import com.jason.cloud.engine.share.driver.user.bean.UserBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/enterprise")
public class EnterpriseController {

    private static final Logger logger = LoggerFactory.getLogger(EnterpriseController.class);

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private PackService packService;

    /**
     * 列举
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getPackByLimit(@RequestParam int page, @RequestParam int limit) {

        //加载所有
        List<EnterpriseBean> enterpriseList = this.enterpriseService.getEnterpriseByLimit((page - 1 ) * limit, limit);
        int count = this.enterpriseService.getEnterpriseCount();

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", count);

        //遍历数据
        JSONArray jsonArrayEnterprise = new JSONArray();
        EnterpriseBean enterpriseBean = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PackBean packBean = null;
        for(Iterator itr = enterpriseList.iterator(); itr.hasNext();) {
            enterpriseBean = (EnterpriseBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", enterpriseBean.getId());
            json.put("name", enterpriseBean.getName());
            packBean = this.packService.queryPackById(enterpriseBean.getPackId().trim());
            json.put("packId", packBean.getName());
            json.put("registerDate", simpleDateFormat.format(enterpriseBean.getRegisterDate()));
            json.put("beginDate", simpleDateFormat.format(enterpriseBean.getBeginDate()));
            json.put("endDate", simpleDateFormat.format(enterpriseBean.getEndDate()));
            jsonArrayEnterprise.add(json);
        }
        jsonObject.put("data", jsonArrayEnterprise);
        return jsonObject;
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject queryEnterpriseById(@PathVariable String id) {

        //取值
        EnterpriseBean enterpriseBean = this.enterpriseService.queryEnterpriseById(id.trim());

        //返回值
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("status", 200);
        jsonObject1.put("msg", "");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("id", enterpriseBean.getId());
        jsonObject2.put("name", enterpriseBean.getName());
        PackBean packBean = this.packService.queryPackById(enterpriseBean.getPackId());
        jsonObject2.put("packId", packBean.getName() + "( 项目数:" + packBean.getProjects() + "、 人员数:" + packBean.getUsers() + " )");
        jsonObject2.put("registerDate", simpleDateFormat.format(enterpriseBean.getRegisterDate()));
        jsonObject2.put("beginDate", simpleDateFormat.format(enterpriseBean.getBeginDate()));
        jsonObject2.put("endDate", simpleDateFormat.format(enterpriseBean.getEndDate()));
        jsonObject2.put("copyrightZh", enterpriseBean.getCopyrightZh());
        jsonObject2.put("copyrightEn", enterpriseBean.getCopyrightEn());

        jsonObject1.put("data", jsonObject2);

        return jsonObject1;
    }

    /**
     * 编辑企业
     * @param json
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject updateEnterpriseParam(@RequestBody JSONObject json) {
        //取值
        String id = json.getString("id");
        String name = json.getString("name");
        String copyrightZh = json.getString("copyrightZh");
        String copyrightEn = json.getString("copyrightEn");

        //返回值
        JSONObject jsonObject = new JSONObject();

        //加载
        EnterpriseBean enterpriseBean = this.enterpriseService.queryEnterpriseById(id.trim());

        //设置
        enterpriseBean.setName(name);
        enterpriseBean.setCopyrightZh(copyrightZh);
        enterpriseBean.setCopyrightEn(copyrightEn);

        //保存
        this.enterpriseService.updateEnterpriseParam(enterpriseBean);

        //返回值
        jsonObject.put("status", 200);
        jsonObject.put("msg", "企业名称修改成功");

        return jsonObject;
    }
}
